package com.lineage.server.clientpackets;

import com.lineage.data.event.TimeTrade;
import com.lineage.echo.ClientExecutor;
import com.lineage.server.datatables.ItemRestrictionsTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1PetInstance;
import com.lineage.server.model.L1Trade;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_TradeAddItem extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_TradeAddItem.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      pc.isGhost();
      if (pc.isDead() || pc.isTeleport())
        return; 
      int itemObjid = readD();
      long itemcount = readD();
      if (itemcount > 2147483647L)
        itemcount = 2147483647L; 
      itemcount = Math.max(0L, itemcount);
      L1ItemInstance item = pc.getInventory().getItem(itemObjid);
      if (item == null)
        return; 
      if (item.getCount() <= 0L)
        return; 
      if ((item.getItemId() == 40308 || item.getItemId() == 44070) && pc.getLevel() < 35) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("预防作弊启动，35级以下无法做此动作"));
        return;
      } 
      if (item.getItemId() == 47011 && pc.getLevel() < 52)
        return; 
      if (!pc.isGm()) {
        if (!item.getItem().isTradable()) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
          return;
        } 
        if (item.getBless() >= 128) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
          return;
        } 
        if (item.isSeal()) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(item.getLogName()) + "处于绑定状态！"));
          return;
        } 
        if (TimeTrade.START && item.get_time() != null) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
          return;
        } 
        if (ItemRestrictionsTable.get().checkItemRestrictions(item.getItemId(), pc)) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
          return;
        } 
      } 
      if (item.isEquipped()) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(141));
        return;
      } 
      Object[] petlist = pc.getPetList().values().toArray();
      Object[] array;
      int length = (array = petlist).length;
      int i = 0;
      while (i < length) {
        Object petObject = array[i];
        if (petObject instanceof L1PetInstance) {
          L1PetInstance pet = (L1PetInstance)petObject;
          if (item.getId() == pet.getItemObjId()) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1187));
            return;
          } 
        } 
        i++;
      } 
      if (item.getId() == pc.getQuest().get_step(74)) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getName()));
        return;
      } 
      if (pc.getDoll(item.getId()) != null) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(1181));
        return;
      } 
      if (pc.get_power_doll() != null && pc.get_power_doll().getItemObjId() == item.getId())
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(1181)); 
      L1PcInstance tradingPartner = (L1PcInstance)World.get().findObject(pc.getTradeID());
      if (tradingPartner == null)
        return; 
      if (pc.getTradeOk())
        return; 
      if (tradingPartner.getInventory().checkAddItem(item, itemcount) != 0) {
        tradingPartner.sendPackets((ServerBasePacket)new S_ServerMessage(270));
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(271));
        return;
      } 
      L1Trade trade = new L1Trade();
      if (itemcount <= 0L) {
        _log.error("要求增加交易物品传回数量小于等于0: " + pc.getName() + ":" + pc.getNetConnection().kick());
        return;
      } 
      trade.tradeAddItem(pc, itemObjid, (int)itemcount);
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
